//
//  ViewController.m
//  01-掌握-corelocation框架的基本使用—定位
//
//  Created by J on 16/2/27.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>{
    CLLocation *_lastLocal;
}
@property(nonatomic,strong)CLLocationManager* manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   }

- (CLLocationManager *)manager{
    
    if (!_manager) {
        //1创建位置管理者
        CLLocationManager* manager = [CLLocationManager new];
        self.manager = manager;
        //1.1 代理 通知 block
        manager.delegate = self;
        
        //每隔多远定位一次
//        [manager setDistanceFilter:100.0];
        
//        kCLLocationAccuracyBestForNavigation//最适合导航
//        kCLLocationAccuracyBest;//最好的
//        kCLLocationAccuracyNearestTenMeters;/10m
//        kCLLocationAccuracyHundredMeters;//100m
//        kCLLocationAccuracyKilometer;//1000m
//        kCLLocationAccuracyThreeKilometers;//3000m
        //精确度越高 越耗电 定位时间越长
        
        if( [[UIDevice currentDevice].systemVersion floatValue]>=8.0){
            
            //        [manager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
            [manager requestAlwaysAuthorization];//前后台定位授权
//            [manager requestWhenInUseAuthorization];//前台定位授权
//            manager.allowsBackgroundLocationUpdates =YES;
        }
        
        //如果能影响酒调用
//        if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
//            
//            //        [manager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//            [manager requestAlwaysAuthorization];//前后台定位授权
//            //        [manager requestWhenInUseAuthorization];//前台定位授权
//        }
        
        
        
    }
    
    return _manager;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
    //纬度相差一度 相差110KM
//    CLLocation* loca1 =[[CLLocation alloc]initWithLatitude:21.123 longitude:12.0];
//    
//    CLLocation* loca2 =[[CLLocation alloc]initWithLatitude:20.123 longitude:12.0];
//    
//   CLLocationDistance dis =  [loca1 distanceFromLocation:loca2];
//
//    NSLog(@"%f",dis);
    
    [self.manager startUpdatingLocation];
    
}


#pragma mark --CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    NSLog(@"%@",error);
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{//关闭定位  和重不定位都显示为 拒绝 kCLAuthorizationStatusDenied 所以需要判断区分
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位被关闭");
    }else if (status == kCLAuthorizationStatusDenied){
        NSLog(@"定位拒绝");
        
    }
    NSLog(@"%zd",status);
    
    
}



///  更新到位置后调用
///
///  @param manager   位置管理者
///  @param locations 位置数组
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //CLLocation
    
//    NSLog(@"%@",[locations lastObject]);
    /*
     latitude 纬度
     longitude 经度
     */
    CLLocation* location = [locations lastObject];
    NSMutableString* result = [NSMutableString string];
    //1.获取方向
    //拿到位置,做一些业务逻辑操作
    NSString* direction ;
    switch ((int)location.course/90) {
        case 0:
        {
            direction = @"北偏东";
        }
            break;
        case 1:
        {
            direction = @"东偏南";
        }
            break;
        case 2:
        {
            direction = @"南偏西";
        }
            break;
        case 3:
        {
            direction = @"西偏北";
        }
            break;
    }
    

    //停止更新
//    [manager stopUpdatixngLocation];
//    打印当前用户的行走方向,偏离角度以及对应的行走距离,
    //2.偏离角度
    NSUInteger angle = (int)location.course%90;
//    NSLog(@"%zd",angle);
    //正方向
    if (angle==0) {//如果%90=0就是正方向
       direction =  [NSString stringWithFormat:@"正%@",[direction substringWithRange:NSMakeRange(0, 1)]];
    }
    //3.距离
    CLLocationDistance dis;
    if (_lastLocal) {//判断是否有上一次的位置
        
        dis =  [[locations lastObject] distanceFromLocation:_lastLocal];
    }
    _lastLocal = location;//保存本次位置
    [result appendFormat:@"%@ %zd度 移动%f米",direction,angle,dis];
    //4.拼接结果
    NSLog(@"%@",result);
   
}



@end
